package com.weigee.ktv.view.fragment

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.linde.library.util.ContextUtil
import com.linde.library.util.DisplayUtil
import com.linde.library.util.ListUtil
import com.linde.library.util.ToastUtil
import com.weigee.ktv.R
import com.weigee.ktv.presenter.ApplySupermarketOrderPresenter
import com.weigee.ktv.presenter.adapter.pager.ApplySupermarketOrderPagerAdapter
import com.weigee.ktv.view.interfaces.ApplySupermarketOrderView
import kotlinx.android.synthetic.main.activity_apply_supermarket_order.*
import kotlinx.android.synthetic.main.custom_tab_apply_supermarket_order.view.*
import kotlinx.android.synthetic.main.include_status_bar_height.*
import kotlinx.android.synthetic.main.include_title_bar.*

/**
 * Created by linde on 2017/6/13.
 * LinDe
 */
class ApplySupermarketOrderFragment : BaseFragment(), ApplySupermarketOrderView, TabLayout.OnTabSelectedListener
{
    var presenter: ApplySupermarketOrderPresenter? = null
    var fragmentAdapter: ApplySupermarketOrderPagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        presenter = ApplySupermarketOrderPresenter(this)
        presenter?.onCreate(savedInstanceState)
        presenter?.onCheckArguments(arguments)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        return inflater?.inflate(R.layout.activity_apply_supermarket_order, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)

        DisplayUtil.setStatusBarHeight(linear_status_bar_height)

        my_title_bar.leftIconClickBack = activity
        my_title_bar.title = "选择包房"

        initTabLayout()
        initListener()
    }

    private fun initListener()
    {
        tv_send_room.setOnClickListener {
            ToastUtil.show(tv_send_room, "确定配送至此包房")
        }
        tv_booking.setOnClickListener {
            ToastUtil.show(tv_booking, "不在包房，我想预先订购")
        }
    }

    private fun initTabLayout(firstPosition: Int = 0)
    {
        fragmentAdapter = ApplySupermarketOrderPagerAdapter(childFragmentManager)
        ListUtil.initTabLayout(tab_layout, view_pager, fragmentAdapter)

        var tab: TabLayout.Tab?
//        var i = 0
//        val size = fragmentAdapter?.count ?: 0
        fragmentAdapter?.let {
            for (index in 0..it.count - 1)
            {
                tab = tab_layout.getTabAt(index)
                tab?.setCustomView(R.layout.custom_tab_apply_supermarket_order)
                tab?.customView?.text_view?.text = fragmentAdapter?.getPageTitle(index)
                if (index == firstPosition)
                {
                    tab?.customView?.civ_bg?.visibility = View.VISIBLE
                    tab?.customView?.text_view?.setTextColor(ContextUtil.getColor(activity, R.color.skinCustomTabApplySupermarketOrder_tabTextSelected))
                }
                else
                {
                    tab?.customView?.civ_bg?.visibility = View.GONE
                    tab?.customView?.text_view?.setTextColor(ContextUtil.getColor(activity, R.color.skinCustomTabApplySupermarketOrder_tabText))
                }
            }
        }
        tab_layout.addOnTabSelectedListener(this)
    }

    override fun onTabReselected(tab: TabLayout.Tab?)
    {
    }

    override fun onTabUnselected(tab: TabLayout.Tab?)
    {
        tab?.customView?.civ_bg?.visibility = View.GONE
        tab?.customView?.text_view?.setTextColor(ContextUtil.getColor(activity, R.color.skinCustomTabApplySupermarketOrder_tabText))
    }

    override fun onTabSelected(tab: TabLayout.Tab?)
    {
        tab?.customView?.civ_bg?.visibility = View.VISIBLE
        tab?.customView?.text_view?.setTextColor(ContextUtil.getColor(activity, R.color.skinCustomTabApplySupermarketOrder_tabTextSelected))
    }

    override fun taskPreview()
    {
    }

    override fun taskCompletely()
    {
    }
}